<?php

namespace App\Http\Controllers\Api;

use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Collection;

class CellularSupportController extends Controller
{
    public function getRealtimeSupportText()
    {
        $webUrl = "https://www.apple.com/cn/watch/cellular/";
        $guzzle = new Client();
        $content = $guzzle->get($webUrl, [
            "headers" => [
                "User-Agent" => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36",
                "Host" => "www.apple.com",
                "Referer" => "https://www.apple.com/cn/watch/cellular/",
            ]
        ]);
        $htmlContent = $content->getBody()->getContents();
        $htmlContent = strip_tags($htmlContent, "<ul><li><section><span>");
        $htmlContent = explode("<section class=\"section section-table\" data-analytics-section-engagement=\"name:table\">", $htmlContent);
        $htmlContent = explode("</section>", $htmlContent[1])[0];
        $htmlContent = str_replace("\n", "", $htmlContent);
        $htmlContent = str_replace("\r\n", "", $htmlContent);
        $htmlContent = str_replace("\t", "", $htmlContent);
        $htmlContent = preg_replace('/ class="([^\"]*)"/isU', '', $htmlContent);
        $htmlContent = explode("><", $htmlContent);
        foreach ($htmlContent as &$item) {
            if ($item[0] != "<") {
                $item = "<" . $item;
            }
            if ($item[-1] != ">") {
                $item = $item . ">";
            }
        }
        $htmlContent = collect($htmlContent);
        $htmlContent = $htmlContent->filter(function ($item, $key) {
            return $key > 43 && !in_array($item, ["<span>", "</span>", "<ul>", "</ul>", "</li>", "<li>"])
                && !in_array(trim($item, "</spanli>"), ["国家或地区", "支持的运营商", "1900 MHz", "1700 MHz", "850 MHz", "UMTS", "41</span> (TD 2500)", "26</span> (800 MHz)",
                    "25</span> (1900 MHz)", "19</span> (800 MHz)", "18</span> (800 MHz)", "17</span> (700b MHz)", "13</span> (700c MHz)", "12</span> (700 MHz)", "5</span> (850 MHz)", "4</span> (AWS)",
                    "2</span> (1900 MHz)", "LTE", "A1976 型号 (44 毫米)", "A1975 型号 (40 毫米)", "Apple Watch Series&nbsp;4", "频段支持", "表款型号"]);
        })->map(function ($item) {
            if (strpos($item, '<li>') !== false && strpos($item, "</li>") === false) {
                $item = $item . "</li>";
            }
            return $item;
        })->values();
        $arrContent = [];
        for ($i = 0; $i < count($htmlContent); $i++) {
            $item = $htmlContent[$i];
            if (($i == 0 || (strpos($item, "<span>") === 0 && !isset($arrContent[trim($htmlContent[$i - 1], "</span>")])))) {
                $item = trim($item, "</span>");
                $arrContent[$item] = [];
            } else if (strpos($item, "<span>") === 0) {
                $item = trim($item, "</span>");
                $arrContent[array_last(array_keys($arrContent))]['tips'] = $item;
            } else {
                $item = trim($item, "</li>");
                $arrContent[array_last(array_keys($arrContent))]['supports'][] = $item;
            }
        }
        $arrContent = $this->trimSpan($arrContent);
        foreach ($arrContent as $key => &$arrSingle) {
            $arrSingle['area'] = $key;
        }
        return array_values($arrContent);
    }

    public function trimSpan($arrItem)
    {
        if (is_array($arrItem)) {
            foreach ($arrItem as &$single) {
                $single = $this->trimSpan($single);
            }
            return $arrItem;
        } else {
            $arrItem = str_replace("<span>", "", $arrItem);
            $arrItem = str_replace("</span>", "", $arrItem);
            $arrItem = str_replace("</span", "", $arrItem);
            return $arrItem;
        }
    }
}
